package SpLib.Com
{	
	import SpLib.Tools.Tr;
	
	import com.adobe.crypto.MD5Stream;
	import com.adobe.serialization.json.JSON;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.ByteArray;

	public class Com extends flash.display.Sprite
	{		
		public static const DEFCHARSET:String = "UTF-8";
		
		private static var s_Instance:Com = null;
		private static var s_RootPath:String = "";
		private static var s_nFPS:int = 20;
		private var m_szUpdateFunc:Array = new Array();
		
		public static function GetInstance():Com
		{
			var iname:String = "SpLib.Com.Com";
			if(s_Instance==null)
			{
				s_Instance = SingletonFactory.Get(iname) as SpLib.Com.Com;
				if(s_Instance==null)
				{			
					s_Instance = new Com();
					SingletonFactory.Reg(iname,s_Instance);
				}
			}
			
			return s_Instance;
		}
		
		public static function get rootpath():String
		{
			return s_RootPath;
		}
		
		public static function set rootpath(path:String):void
		{
			s_RootPath = path;
		}
		
		public static function get fps():int
		{
			return s_nFPS;
		}
		
		public static function set fps(f:int):void
		{
			s_nFPS = f;
		}
		
		public static function AddUpdateFunc(func:Function):void
		{
			GetInstance().m_szUpdateFunc.push(func);
		}
		
		public function Com()
		{
			if(SpLib.Com.Com.s_Instance!=null)
			{
				throw new Error("Com is Singleton");
			}
			
			SpLib.Tools.Tr.GetInstance();
			this.addEventListener(Event.ENTER_FRAME,OnUpdate);
		}

		private function OnUpdate(evt:Event):void
		{
			var i:int;
			for(i=0;i<m_szUpdateFunc.length;i++)
			{
				var func:Function = m_szUpdateFunc[i] as Function;
				func();
			}
		}
		
		public static function GetArraySub(array:Array,idx:int,type:String,def:*):*
		{
			if(array==null || array.length<=idx)
			{
				return def;
			}
			
			if(typeof(array[idx])==type)
			{
				return array[idx];
			}
			
			return def;
		}
		
		public static function PopLine(data:String):Array
		{
			var result:Array = new Array;
			
			var dlen:int = 2;
			var pos:int = data.search("\r\n");
			if(pos<0)
			{
				dlen = 1;
				pos = data.search("\n");
				if(pos<0)
				{
					result.push(data);
					result.push("");
					return result;
				}
			}	
			
			result.push(data.substr(0,pos));
			result.push(data.substr(pos+dlen));
			
			return result;
		}
		
		public static function GetFileType(filename:String):String
		{
			var pos:int = filename.lastIndexOf(".");
			return filename.substr(pos+1);
		}
		
		public static function GetStringMD5(str:String,charset:String="UTF-8"):String
		{
			var bytes:ByteArray = new ByteArray();
			bytes.writeMultiByte(str,charset);
			var md5:com.adobe.crypto.MD5Stream = new com.adobe.crypto.MD5Stream();
			return md5.complete(bytes);
		}
		
		public static function Array2Str(agv:Array):String
		{
			return JSON.encode(agv);
		}
		
		public static function CompareStr(rh1:String,rh2:String):int
		{	
			if(rh1.length>rh2.length) return 1;
			if(rh1.length<rh2.length) return -1;
			
			var i:int;
			var rhc1:Number,rhc2:Number;
			for(i=0;i<rh1.length;i++)
			{
				rhc1 = rh1.charCodeAt(i);
				rhc2 = rh2.charCodeAt(i);
				
				if(rhc1!=rhc2)
				{
					return rhc1>rhc2?1:-1;
				}
			}
			
			return 0;
		}
	}
	
	
}
